const { bfmetaSDK } = require('../../util/util')

// 通过助记词返回账户地址和公私钥对
const signIn = () => {
    return async (req, res, next) => {
        try {
            if(!req.body.words){
                return res.status(400).send({
                    success: false,
                    message: "参数不能为空"
                })
            }
            if(typeof req.body.words !== 'string'){
                return res.status(400).send({
                    success: false,
                    message: "参数类型必须为string"
                })
            }
            // 根据助记词生成公私钥对
            const keypair = await bfmetaSDK.bfchainSignUtil.createKeypair(req.body.words)
            // 发行事件需要16进制字符串的公钥
            const secretKey = keypair.secretKey.toString("hex")  
            const publicKey = keypair.publicKey.toString("hex")
            // 通过公钥获取地址 
            const address = await bfmetaSDK.bfchainSignUtil.getAddressFromPublicKey(keypair.publicKey)
            // 返回三个数据 json格式
            res.json({address, secretKey, publicKey})
        } catch (error) {
            next(error)
        }
    }
       
}

module.exports = { signIn }